package com.investment.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.investment.model.common.Result;
import com.investment.model.entity.Factor;
import com.investment.service.FactorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 因子控制器
 */
@RestController
@RequestMapping("/factors")
@Tag(name = "因子控制器", description = "因子管理相关接口")
public class FactorController {

    @Autowired
    private FactorService factorService;

    @GetMapping
    @Operation(summary = "分页查询因子列表", description = "分页查询因子列表接口")
    public Result<IPage<Factor>> list(
            @RequestParam Map<String, Object> params,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size) {
        IPage<Factor> pageData = factorService.listByPage(params, page, size);
        return Result.success(pageData);
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询因子详情", description = "查询因子详情接口")
    public Result<Map<String, Object>> detail(@PathVariable Long id) {
        Map<String, Object> factorDetail = factorService.getFactorDetail(id);
        return Result.success(factorDetail);
    }

    @GetMapping("/code/{code}")
    @Operation(summary = "根据因子代码查询因子", description = "根据因子代码查询因子接口")
    public Result<Factor> getByCode(@PathVariable String code) {
        Factor factor = factorService.getByCode(code);
        return Result.success(factor);
    }

    @GetMapping("/type/{type}")
    @Operation(summary = "根据类型查询因子列表", description = "根据类型查询因子列表接口")
    public Result<List<Factor>> listByType(@PathVariable Integer type) {
        List<Factor> factors = factorService.listByType(type);
        return Result.success(factors);
    }

    @GetMapping("/creator/{creatorId}")
    @Operation(summary = "根据创建者查询因子列表", description = "根据创建者查询因子列表接口")
    public Result<List<Factor>> listByCreator(@PathVariable Long creatorId) {
        List<Factor> factors = factorService.listByCreatorId(creatorId);
        return Result.success(factors);
    }

    @PostMapping
    @Operation(summary = "添加因子", description = "添加因子接口")
    public Result<Long> add(@RequestBody Factor factor) {
        Long factorId = factorService.addFactor(factor);
        return Result.success(factorId);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新因子", description = "更新因子接口")
    public Result<Boolean> update(@PathVariable Long id, @RequestBody Factor factor) {
        factor.setId(id);
        boolean success = factorService.updateFactor(factor);
        return Result.success(success);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除因子", description = "删除因子接口")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean success = factorService.deleteFactor(id);
        return Result.success(success);
    }

    @DeleteMapping("/batch")
    @Operation(summary = "批量删除因子", description = "批量删除因子接口")
    public Result<Boolean> batchDelete(@RequestBody List<Long> ids) {
        boolean success = factorService.batchDeleteFactor(ids);
        return Result.success(success);
    }
} 